
function initRemuneraciones() {
    remuneraciones = document.getElementById("remuneraciones");
    all_remuneraciones = remuneraciones.getElementsByTagName("div");
    numeroRemuneraciones = all_remuneraciones.length;
    numeroTotalFuentes = remuneraciones.getElementsByTagName("p").length;
}

function eliminarRemuneracion(element_id) {
    var element = document.getElementById(element_id);
    var parent = element.parentNode;

    parent.removeChild(element);
}

function eliminarFuente(element_id) {
    var element = document.getElementById(element_id);
    var parent = element.parentNode;
    parent.removeChild(element);

    var num_id = element_id.split("_")[1];
    var numFuentes = document.getElementById("remuneracionNumFuentesFinanciamiento_" + num_id);
    numFuentes.value = parseInt(numFuentes.value) - 1;
}

function select_choice(selection,field_id) {
    var textfield = document.getElementById(field_id);

    if(selection.value == 'otro') {
        textfield.value = '';
        textfield.style.visibility  = 'visible';
        textfield.focus();
    } else {
        textfield.style.visibility  = 'hidden';
        textfield.value = selection.value;
    }
}

function tipo_financiamiento_choice(select,remuneracion_id) {

    var div_fuentes = document.getElementById(remuneracion_id + "_fuentes");
    var boton = document.getElementById(remuneracion_id + "_botonNuevaFuente");
    div_fuentes.innerHTML = "";
    var numFuentes = document.getElementById("remuneracionNumFuentesFinanciamiento_" + remuneracion_id.split("_")[1]);
    numFuentes.value = "0";

    if(select.value == "Total") {
        agregarFuenteFinanciamiento(remuneracion_id)
        boton.style.display  = "none";
    } else if(select.value == "Co-financiamiento") {
        agregarFuenteFinanciamiento(remuneracion_id)
        agregarFuenteFinanciamiento(remuneracion_id)
        boton.style.display = "inline";
    } else {
        boton.style.display = "none";
    }

}

function frecuencia_choice(select,p_periodo_id) {
    var p_periodo = document.getElementById(p_periodo_id);
    var num_id = p_periodo_id.split("_")[1];

    p_periodo.innerHTML = "";

    var label1 = document.createElement("label");
    label1.innerHTML = "Inicio"
    label1.htmlFor = "remuneracionFechaInicio_" + num_id;
    var input1 = document.createElement("input");
    input1.type = "text";
    input1.id = "remuneracionFechaInicio_" + num_id;
    input1.name = "remuneracionFechaInicio";
    input1.size = "10";
    input1.maxLength = "10";
    input1.onchange= function() {return validarFecha(this);};
    var img1 = document.createElement("img");
    img1.src = "../css/images/calendar_edit.png";
    img1.alt = "Calendario";
    img1.onclick = function(event) {scwShow(document.getElementById("remuneracionFechaInicio_" + num_id),event);};

    var label2 = document.createElement("label");
    label2.innerHTML = "Fin"
    label2.htmlFor = "remuneracionFechaFin_" + num_id;
    var input2 = document.createElement("input");
    input2.type = "text";
    input2.id = "remuneracionFechaFin_" + num_id;
    input2.name = "remuneracionFechaFin";
    input2.size = "10";
    input2.maxLength = "10";
    input2.onchange= function() {return validarFecha(this);};
    var img2 = document.createElement("img");
    img2.src = "../css/images/calendar_edit.png";
    img2.alt = "Calendario";
    img2.onclick = function(event) {scwShow(document.getElementById("remuneracionFechaFin_" + num_id),event);};

    if (select.value == "Unico") {
        label1.innerHTML = "Fecha";
        input2.type = "hidden";
        p_periodo.appendChild(label1);
        p_periodo.appendChild(document.createTextNode("\u00A0"));
        p_periodo.appendChild(input1);
        p_periodo.appendChild(document.createTextNode("\u00A0"));
        p_periodo.appendChild(img1);
        p_periodo.appendChild(input2);
    } else {
        p_periodo.appendChild(document.createTextNode("Periodo:\u00A0"));
        p_periodo.appendChild(label1);
        p_periodo.appendChild(document.createTextNode("\u00A0"));
        p_periodo.appendChild(input1);
        p_periodo.appendChild(document.createTextNode("\u00A0"));
        p_periodo.appendChild(img1);
        p_periodo.appendChild(document.createTextNode("\u00A0\u00A0"));
        p_periodo.appendChild(label2);
        p_periodo.appendChild(document.createTextNode("\u00A0"));
        p_periodo.appendChild(input2);
        p_periodo.appendChild(document.createTextNode("\u00A0"));
        p_periodo.appendChild(img2);
    }
}

function agregarRemuneracion() {
    var count = numeroRemuneraciones;

    var remuneracion = document.createElement("div");
    remuneracion.id = "remuneracion_" + count;
    remuneraciones.appendChild(remuneracion);

    var p1 = document.createElement("p");
    remuneracion.appendChild(p1);
    var label1 = document.createElement("label");
    p1.appendChild(label1);
    label1.htmlFor = "remuneracionConceptoSelect_" + count;
    label1.innerHTML = "Concepto";
    p1.appendChild(document.createTextNode("\u00A0"));
    var select1 = document.getElementById("remuneracion_conceptos").cloneNode(true);
    p1.appendChild(select1);
    select1.id = "remuneracionConceptoSelect_" + count;
    select1.style.display = "inline";
    select1.onchange = function() {select_choice(this,"remuneracionConcepto_" + count);};
    p1.appendChild(document.createTextNode("\u00A0"));
    var remuneracionConcepto = document.createElement("input");
    p1.appendChild(remuneracionConcepto);
    remuneracionConcepto.type = "text";
    remuneracionConcepto.id = "remuneracionConcepto_" + count;
    remuneracionConcepto.name = "remuneracionConcepto";
    remuneracionConcepto.style.visibility = "hidden";
    p1.appendChild(document.createTextNode("\u00A0"));
    var a = document.createElement("a");
    p1.appendChild(a);
    a.href = "javascript:eliminarRemuneracion('remuneracion_" + count + "');";
    a.innerHTML = "Eliminar Remuneraci\u00f3n";

    var p2 = document.createElement("p");
    remuneracion.appendChild(p2);
    var label2 = document.createElement("label");
    p2.appendChild(label2);
    label2.htmlFor = "remuneracionFrecuencia_" + count;
    label2.innerHTML = "Frecuencia de Pago";
    p2.appendChild(document.createTextNode("\u00A0"));
    var select2 = document.createElement("select");
    p2.appendChild(select2);
    select2.id = "remuneracionFrecuencia_" + count;
    select2.name = "remuneracionFrecuencia";
    select2.options[0] = new Option("Seleccione","");
    select2.options[1] = new Option("Unico","Unico");
    select2.options[2] = new Option("Mensual","Mensual");
    select2.options[3] = new Option("Trimestral","Trimestral");
    select2.options[4] = new Option("Semestral","Semestral");
    select2.options[5] = new Option("Anual","Anual");
    select2.onchange = function() {frecuencia_choice(this,"remuneracionPeriodo_" + count);};

    var p3 = document.createElement("p");
    remuneracion.appendChild(p3);
    p3.id = "remuneracionPeriodo_" + count;

    var p4 = document.createElement("p");
    remuneracion.appendChild(p4);
    var label3 = document.createElement("label");
    p4.appendChild(label3);
    label3.htmlFor = "remuneracionTipoFinanciamiento_" + count;
    label3.innerHTML = "Tipo de Financiamiento";
    p2.appendChild(document.createTextNode("\u00A0"));
    var select3 = document.createElement("select");
    p4.appendChild(select3);
    select3.id = "remuneracionTipoFinanciamiento_" + count;
    select3.name = "remuneracionTipoFinanciamiento";
    select3.options[0] = new Option("Seleccione","");
    select3.options[1] = new Option("Total","Total");
    select3.options[2] = new Option("Co-financiamiento","Co-financiamiento");
    select3.onchange = function() {tipo_financiamiento_choice(this,"remuneracion_" + count);};

    var div1 = document.createElement("div");
    remuneracion.appendChild(div1);
    div1.className = "form_fuente_financiamiento";
    var input1 = document.createElement("input");
    div1.appendChild(input1);
    input1.type ="hidden";
    input1.id = "remuneracionNumFuentesFinanciamiento_" + count;
    input1.name = "remuneracionNumFuentesFinanciamiento";
    var div2 = document.createElement("div");
    div1.appendChild(div2);
    div2.id = "remuneracion_" + count + "_fuentes";
    var p5 = document.createElement("p");
    div1.appendChild(p5);
    var input2 = document.createElement("input");
    p5.appendChild(input2);
    input2.type = "button";
    input2.value = "Nueva Fuente de Financiamiento";
    input2.id = "remuneracion_" + count + "_botonNuevaFuente"
    input2.onclick = function() {agregarFuenteFinanciamiento("remuneracion_" + count);};
    input2.style.display = "none";

    remuneracion.appendChild(document.createElement("hr"));

    numeroRemuneraciones++;
}

function agregarFuenteFinanciamiento(remuneracion_id) {

    var div_fuentes = document.getElementById(remuneracion_id + "_fuentes");
    var all_fuentes = div_fuentes.getElementsByTagName("p");
    var count = numeroTotalFuentes;
    var num_id = remuneracion_id.split("_")[1];

    var p = document.createElement("p");
    p.id = remuneracion_id + "_fuenteWrap_" + count;
    div_fuentes.appendChild(p);

    var label1 = document.createElement("label");
    p.appendChild(label1);
    label1.htmlFor = remuneracion_id + "_fuenteSelect_" + count;
    label1.innerHTML = "Fuente de Financiamiento";
    var select1 = document.getElementById("fuentes_financiamiento").cloneNode(true);
    p.appendChild(select1);
    select1.id = remuneracion_id + "_fuenteSelect_" + count;
    select1.style.display = "inline";
    select1.onchange = function() {select_choice(this, remuneracion_id + "_fuente_" + count);};
    var input1 = document.createElement("input");
    p.appendChild(input1);
    input1.type = "text";
    input1.id = remuneracion_id + "_fuente_" + count;
    input1.name = "fuenteFinanciamiento";
    input1.style.visibility = "hidden";
    p.appendChild(document.createTextNode("\u00A0"));
    var a = document.createElement("a");
    p.appendChild(a);
    a.href = "javascript:eliminarFuente('" + remuneracion_id + "_fuenteWrap_" + count + "');";
    a.innerHTML = "Eliminar Fuente";

    p.appendChild(document.createElement("br"));

    var label2 = document.createElement("label");
    p.appendChild(label2);
    label2.htmlFor = remuneracion_id + "_fuente_" + count + "_monto";
    label2.innerHTML = "Monto";
    var input2 = document.createElement("input");
    p.appendChild(input2);
    input2.type = "text";
    input2.id = remuneracion_id + "_fuente_" + count + "_monto";
    input2.name ="fuenteFinanciamientoMonto";
    input2.size = "6";

    p.appendChild(document.createTextNode("\u00A0"));

    var label3 = document.createElement("label");
    p.appendChild(label3);
    label3.htmlFor = remuneracion_id + "_fuente_" + count + "_moneda";
    label3.innerHTML = "Moneda";
    var select2 = document.createElement("select");
    p.appendChild(select2);
    select2.id = remuneracion_id + "_fuente_" + count + "_moneda";
    select2.name = "fuenteFinanciamientoMoneda";
    select2.options[0] = new Option("Seleccione","");
    select2.options[1] = new Option("Bs.F","Bs.F");
    select2.options[2] = new Option("Bs.","Bs.");
    select2.options[3] = new Option("D\u00f3lares","Dolares");
    select2.options[4] = new Option("Euros","Euros");
    select2.options[5] = new Option("Libras","Libras");
    select2.options[6] = new Option("Liras","Liras");
    select2.options[7] = new Option("Marcos","Marcos");
    select2.options[8] = new Option("Pesetas","Pesetas");
    select2.options[9] = new Option("Francos","Francos");
    select2.options[10] = new Option("Pesos","Pesos");
    select2.options[11] = new Option("Yenes","Yenes");
    select2.options[12] = new Option("Yuanes","Yuanes");

    var numFuentes = document.getElementById("remuneracionNumFuentesFinanciamiento_" + num_id);
    numFuentes.value = parseInt(numFuentes.value) + 1;

    numeroTotalFuentes++;
}